<?php

namespace app\topics\middle;

class SettingMiddle extends \app\base\middle\BaseMiddle {


    protected function meta() {
        $this->setMeta('社区设置');
        $this->setName('社区设置');
        $this->setCrumb([
            [
                'name' => '社区设置',
                'url' => url()
            ]
        ]);

        $jobList = target('topics/TopicsUser')->jobs();
        $info = target('topics/TopicsUser')->getWhereInfo([
            'user_id' => $this->params['user_id']
        ]);
        return $this->run([
            'pageInfo' => $this->pageInfo,
            'jobList' => $jobList,
            'info' => $info
        ]);
    }

    protected function putInfo() {
        $post = $this->params['data'];
        $userId = intval($this->params['user_id']);
        $data = [
            'user_id' => $userId,
            'home' => html_clear($post['home']),
            'sign' => html_clear($post['sign']),
            'qq' => html_clear($post['qq']),
            'job_id' => intval($post['job_id']),
        ];
        if($data['home']) {
            if(!preg_match('/http:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is',$data['home'])){
                return $this->stop('个人主页输入错误！');
            }
        }
        if($data['qq']) {
            if (!preg_match('/^[1-9]\d{4,11}$/i', $data['qq'])) {
                return $this->stop('QQ号码输入错误！');
            }
        }

        $jobList = target('topics/TopicsUser')->jobs();
        if($data['job_id']) {
            if(!$jobList[$data['job_id']]) {
                return $this->stop('所选职业不存在！');
            }
        }

        $userInfo = target('topics/TopicsUser')->getWhereInfo([
            'user_id' => $userId
        ]);
        if($userInfo) {
            $data['id'] = $userInfo['id'];
            $status = target('topics/TopicsUser')->edit($data);
        }else {
            $status = target('topics/TopicsUser')->add($data);
        }
        return $this->run([],'保存资料成功！');
    }

    protected function putAvatar() {
        $userId = intval($this->params['user_id']);
        return target('topics/Upload', 'middle')->setParams([
            'user_id' => $userId,
            'width' => 256,
            'height' => 256
        ])->post()->export(function ($data) use($userId) {
            $file = reset($data);
            $status = target('topics/TopicsUser')->edit([
                'user_id' => $userId,
                'avatar' => $file['url']
            ]);
            if (empty($status)) {
                return $this->stop('头像保存失败!');
            }
            return $this->run($data, '头像修改成功!');
        }, function ($message) {
            return $this->stop($message);
        });
    }

    protected function putPassword() {
        $userId = intval($this->params['user_id']);
        $oldPassword = $this->params['old_password'];
        $password = $this->params['password'];
        $password2 = $this->params['password2'];
        $userInfo = target('topics/TopicsUser')->getInfo($userId);
        if (md5($oldPassword) <> $userInfo['password']) {
            return $this->stop('原始密码输入不正确!');
        }
        if (empty($password)) {
            return $this->stop('请输入新密码!');
        }
        if ($password <> $password2) {
            return $this->stop('两次密码输入不正确!');
        }
        $data = [];
        $data['user_id'] = $userId;
        $data['password'] = md5($password);
        if (!target('topics/TopicsUser')->edit($data)) {
            return $this->stop('密码修改失败!');
        }
        return $this->run([], '密码修改成功!');
    }


}